﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DotNet.Utilities
{
    public class webopera
    {
        private AxSHDocVw.AxWebBrowser ax;

        public delegate void openpage(string url);
        public openpage openpagedelegate;
        public delegate void waitload();
        public waitload waitloaddelegate;
        public delegate HtmlElement getelement(string name);
        public getelement getelementdelegate;
        public delegate HtmlDocument getdoc();
        public getdoc getdocdelegate;
        public delegate mshtml.IHTMLElementCollection getByTagName(string name);
        public getByTagName getByTagNamedelegate;
        public delegate mshtml.IHTMLDocument2 getdomdocment();
        public getdomdocment getdomdocmentdelegate;
      
        public webopera(AxSHDocVw.AxWebBrowser web)
        {
            ax = web;
        }

        public void OpenPage(string url)
        {
            if (!ax.InvokeRequired)
            {
                System.Object nullObject = 0;
                string str = "";
                System.Object nullObjStr = str;
                Cursor.Current = Cursors.WaitCursor;
                this.ax.Navigate(url, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
                ax.Silent = true;
                Cursor.Current = Cursors.Default;
               
            }
            else
            {
                openpagedelegate = new openpage(OpenPage);
                ax.Invoke(openpagedelegate, new object[] { url });
            }
            WaitLoad();
        }

        public void WaitLoad()
        {
            DateTime now = DateTime.Now;
            if (ax.InvokeRequired)
            {
                waitloaddelegate = new waitload(WaitLoad);
                ax.Invoke(waitloaddelegate);
            }
            else
            {
               
                while (ax.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                {
                    Application.DoEvents();
                    TimeSpan ts = DateTime.Now.Subtract(now);
                    if (ts.Seconds > 5)
                    {
                        break;
                    }
                }
            }
        }

        public mshtml.IHTMLElement getel(string name)
        {
            int i = 0;
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            while ((mshtml.IHTMLElement)doc.all.item(name, 0) == null)
            {
                Application.DoEvents();
                i++;
                if (i > 100)
                {
                    return null;
                }
            }
            return (mshtml.IHTMLElement)doc.all.item(name, 0);
        }

        public void SetValue(string name,string v)
        {
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            while ((mshtml.IHTMLElement)doc.all.item(name, 0) == null)
            {
                Application.DoEvents();
            }
            mshtml.IHTMLElement el = (mshtml.IHTMLElement)doc.all.item(name, 0);
            el.setAttribute("value", v, 0);
        }

        public mshtml.IHTMLElementCollection Getimages()
        {
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            return doc.images;
        }

        public void ElClick(string name)
        {
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            mshtml.IHTMLElement el = (mshtml.IHTMLElement)doc.all.item(name, 0);
            while (el == null)
            {
                el = (mshtml.IHTMLElement)doc.all.item(name, 0);
                Application.DoEvents();
            }
            el.click();
        }

        public string gethtml()
        {
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            while (doc == null || doc.body == null || doc.body.innerHTML == null )
            {
                doc = (mshtml.IHTMLDocument2)ax.Document;
                Application.DoEvents();
            }
            return doc.body.innerHTML;
        }

        public mshtml.IHTMLDocument2 GetDomdocment()
        {
            if (ax.InvokeRequired)
            {
                getdomdocmentdelegate = new getdomdocment(GetDomdocment);
                return (mshtml.IHTMLDocument2)ax.Invoke(getdomdocmentdelegate);
            }
            else
            {
                return (mshtml.IHTMLDocument2)ax.Document;
            }
        }

        public mshtml.IHTMLElementCollection getbytagname(string name)
        {


            if (ax.InvokeRequired)
            {
                getByTagNamedelegate = new getByTagName(getbytagname);
                return (mshtml.IHTMLElementCollection)ax.Invoke(getByTagNamedelegate, new object[] { name });
            }
            else
            {
                mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
                //return (mshtml.IHTMLElementCollection) doc.all.tags("option");
                return (mshtml.IHTMLElementCollection)doc.all.tags(name);
            }
          
        }


        public mshtml.IHTMLSelectElement GetSelect(string name)
        {
            //mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            //mshtml.HTMLSelectElementClass select = (mshtml.HTMLSelectElementClass)doc.all.item(name, 0);
            //while (select == null)
            //{
            //    select = (mshtml.HTMLSelectElementClass)doc.all.item(name, 0);
            //    Application.DoEvents();
            //}
            //return select;
            return null;
        }

        public bool Havemsg(string msg)
        {
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ax.Document;
            if (doc.body.innerHTML.Contains(msg))
            {
                return true;
            }
            return false;
        }

        public void executeOnChangeHandler(mshtml.IHTMLElement element,string eventname)
        {
            //mshtml.IHTMLElement e = element as mshtml.IHTMLElement;
            //mshtml.IHTMLDocument4 doc4 = e.document as mshtml.IHTMLDocument4;
            //object dummy = null;
            //object eventObj = doc4.CreateEventObject(ref dummy);

            //mshtml.HTMLSelectElementClass se = element as mshtml.HTMLSelectElementClass;
            //se.FireEvent(eventname, ref eventObj);
        }

    }
}
